﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AssetManagementSystem.Model;
using AssetManagementSystem.Utils.Events;
using AssetManagementSystem.View.IView;
using AssetManagementSystem.Utils;

namespace AssetManagementSystem.Presenter {
    class GroupPresenter {

        GroupManager manager;
        IGroupView view;

        public GroupPresenter(IGroupView view) {
            manager = new GroupManager();
            this.view = view;
            //Handle Group View Events
            this.view.Groups_LoadAll += new EventHandler(PresentAllGroups);
            this.view.Groups_Find += new EventHandler<SearchEventArgs>(PresentFoundGroups);
            this.view.Group_Create += new EventHandler<GroupEventArgs>(CreateGroup);
            this.view.Group_Update += new EventHandler<GroupEventArgs>(UpdateGroup);
            this.view.Group_Delete += new EventHandler<GroupEventArgs>(DeleteGroup);
            this.view.GroupProperty_Delete += new EventHandler<GroupPropertiesEventArgs>(DeleteGroupProperties);
        }

        /// <summary>
        /// Provides a list of all groups to the view for display
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event args</param>
        private void PresentAllGroups(object sender, EventArgs e) {
            view.SetGroupList(manager.GetAllGroups());
        }

        /// <summary>
        /// Provides a list of groups found based on search data provided in event args
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event args, must contain search input</param>
        private void PresentFoundGroups(object sender, SearchEventArgs e) {
            view.SetGroupList(manager.FindGroups(e.SearchText));
        }

        /// <summary>
        /// Tries to create a new group and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void CreateGroup(object sender, GroupEventArgs e) {
            try {
                //store the new group
                manager.CreateGroup(e.Group);
                //refresh the groups on the view
                PresentAllGroups(this, null);
                view.ShowMessage("Group successfully saved.");
            } catch (Exception createFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(createFailed.Message);
            }
        }

        /// <summary>
        /// Tries to update an existing group and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void UpdateGroup(object sender, GroupEventArgs e) {
            try {
                //update the group
                manager.UpdateGroup(e.Group);
                //refresh the groups on the view
                PresentAllGroups(this, null);
                view.ShowMessage("Group successfully updated.");
            } catch (Exception updateFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(updateFailed.Message);
            }
        }

        /// <summary>
        /// Tries to delete an existing group and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void DeleteGroup(object sender, GroupEventArgs e) {
            try {
                //delete the group
                manager.DeleteGroup(e.Group);
                //refresh the groups on the view
                PresentAllGroups(this, null);
                view.ShowMessage("Group successfully deleted.");
            } catch (Exception deleteFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(deleteFailed.Message);
            }
        }

        /// <summary>
        /// Tries to delete all properties of an group and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void DeleteGroupProperties(object sender, GroupPropertiesEventArgs e) {
            try {
                //delete the group
                manager.DeleteGroupProperties(e.GroupName, e.Properties);
                //refresh the groups on the view
                PresentAllGroups(this, null);
            } catch (Exception deleteFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(deleteFailed.Message);
            }
        }

    }
}
